package com.itheima.stargraph.handler;

import cn.hutool.json.JSONUtil;
import com.itheima.stargraph.pojo.MessageBase;
import com.itheima.stargraph.service.ComfyuiMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class ComfyuiMessageHandler extends TextWebSocketHandler {

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("=============连接成功");
    }

    @Autowired
    private ComfyuiMessageService comfyuiMessageService;

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("=============收到消息:"+message.getPayload());
        MessageBase messageBase = JSONUtil.toBean(message.getPayload(), MessageBase.class);

        //推送消息
        comfyuiMessageService.handleMsg(messageBase);
    }

}